package view.plant
{
	/**
	 *
	 *@author CrazyChen 
	 *2013-4-10
	 */	
	import data.Globals;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import manager.EffectName;
	import manager.ObjectPool;
	
	public class PlantItem extends Sprite
	{
		private var bmp:Bitmap;
		
		private var _maxHp:int;
		
		private var _maxEnergy:int;
		
		private var _blood:int;
		
		private var _energy:int;
		
		public var gradAry:Array;
		
		private var bloodBar:BloodBarCls;
		
		private var energyBar:EnergyBarCls;
	
		public var onRemove:Function;
		
		private var _showBloodBar:Boolean;
		
		public function PlantItem()
		{
			super();
			gradAry = [];
			this.buttonMode = true;
			bmp = new Bitmap();
			this.addChild(bmp);
			bloodBar = new BloodBarCls();
			bloodBar.mouseChildren = false;
			bloodBar.mouseEnabled = false;
			bloodBar.y = 0;
			this.addChild(bloodBar);
			bloodBar.stop();
			
			energyBar = new EnergyBarCls();
			energyBar.mouseChildren = false;
			energyBar.mouseEnabled = false;
			energyBar.y = 5;
			this.addChild(energyBar);
			energyBar.stop();
			
		}
		
		public function bmd(b:BitmapData,w:int,h:int):void
		{
			bmp.bitmapData = b;
			bmp.width = w;
			bmp.height = h;
			bmp.x = -w/2;
			bmp.y = -h/2;
		}

		public function set blood(value:int):void
		{
			_blood = value;
			//被击中显示血条
			if(blood<_maxHp){
				showBloodBar = true;
			}
			bloodBar.gotoAndStop(int((_maxHp-value)/_maxHp*bloodBar.totalFrames));
			if(value<=0){
				Globals.gameView.calculateScore(_energy);
				Globals.gameView.createExplode(EffectName.A_EXPLODE,this.x,this.y);
				die();
			}
		}
		
		/**
		 *死亡 
		 * 
		 */
		public function die():void
		{
			if(this.parent){
				this.parent.removeChild(this);
				this.x = 0;
				this.y = 0;
				for(var i:int=0;i<gradAry.length;i++){
					var point:Point = gradAry[i];
					Globals.gameView.setGradIsCanSite(point.x,point.y,true);
				}
				onRemove(this);
				ObjectPool.putObj(this);
			}
		}

		public function set maxHp(value:int):void
		{
			_maxHp = value;
			blood = value;
		}
		public function get energy():int
		{
			return _energy;
		}

		public function set energy(value:int):void
		{
			_energy = value;
			energyBar.gotoAndStop(int((maxEnergy-value)/maxEnergy*energyBar.totalFrames));
			if(_energy<_maxEnergy){
				showBloodBar = true;
				if(_energy<0){
					_energy = 0;
				}
			}
		}

		public function get showBloodBar():Boolean
		{
			return _showBloodBar;
		}

		public function set showBloodBar(value:Boolean):void
		{
			_showBloodBar = value;
			bloodBar.visible = value;
			energyBar.visible = value;
		}

		public function get blood():int
		{
			return _blood;
		}

		public function get maxEnergy():int
		{
			return _maxEnergy;
		}

		public function set maxEnergy(value:int):void
		{
			_maxEnergy = value;
		}


	}
}